package com.bw.ivideoproject.video

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bw.ivideoproject.R
import com.bw.ivideoproject.databinding.FragmentVideoBinding
import com.bw.ivideoproject.databinding.ItemVideoBinding
import com.bw.ivideoproject.entity.VideoEntity
import com.bw.lib_imageutils.ImageLoaderManager
import com.bw.lib_imageutils.ImageOptions
import com.bw.lib_mvvm.BaseFragment
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener

class VideoFragment : BaseFragment<FragmentVideoBinding>() {

    private val viewModel: VideoViewModel by lazy {
        ViewModelProvider(this)[VideoViewModel::class.java]
    }
    private lateinit var videoAdapter: VideoAdapter
    private var page = 0
    private var pageSize = 10

    override fun bindLayout(): Int {
        return R.layout.fragment_video
    }

    override fun initView() {
        videoAdapter = VideoAdapter()
        binding.rvVideo.apply {
            this.adapter = videoAdapter
            this.layoutManager = GridLayoutManager(requireContext(),2)
        }
    }

    override fun initData() {
        viewModel.getType()
        binding.tabType.addOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                val pid = tab?.tag as Int
                viewModel.getTypeById(pid)
            }

            override fun onTabUnselected(p0: TabLayout.Tab?) {
            }

            override fun onTabReselected(p0: TabLayout.Tab?) {
            }
        })

        binding.tabTypeId.addOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                val typeId = tab?.tag as Int
                viewModel.getVideoByType(typeId, page, pageSize)
            }

            override fun onTabUnselected(p0: TabLayout.Tab?) {}
            override fun onTabReselected(p0: TabLayout.Tab?) {}
        })
    }

    override fun handleObserver() {
        super.handleObserver()
        viewModel.typeLiveData.observe(this) {
            it.forEach { entity ->
                binding.tabType.addTab(binding.tabType.newTab().setText(entity.typename).setTag(1))
            }
        }

        viewModel.typeByIdLiveData.observe(this) {
            it.forEach { entity ->
                binding.tabTypeId.addTab(
                    binding.tabTypeId.newTab().setText(entity.typename).setTag(entity.id)
                )
            }
            binding.tabTypeId.selectTab(binding.tabTypeId.getTabAt(0))
        }

        viewModel.videoLiveData.observe(this) {
            videoAdapter.refresh(it)
            binding.rvVideo.scrollToPosition(0)
        }

        viewModel.filedLiveData.observe(this) {
            showMsg(it)
        }
    }

    inner class VideoAdapter : RecyclerView.Adapter<VideoAdapter.VideoViewHolder>() {

        var data = mutableListOf<VideoEntity>()

        fun refresh(list:List<VideoEntity>){
            data.addAll(list)
            this.notifyDataSetChanged()
        }

        override fun onCreateViewHolder(parent: ViewGroup, p1: Int): VideoViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.item_video, parent, false)
            return VideoViewHolder(view)
        }

        override fun onBindViewHolder(holder: VideoViewHolder, position: Int) {
            val entity = data[position]
            holder.subBinding.tvName.text = entity.title
            holder.subBinding.tvTitle.text = entity.toutiaolvideotext
            val option =  ImageOptions.Builder().setPlaceImage(R.mipmap.ic_launcher).build()
            ImageLoaderManager.getInstance().loadFileIntoImageView(requireContext(),entity.videomainimag,holder.subBinding.ivVideo,option)

            holder.subBinding.root.setOnClickListener {
                val intent = Intent(requireActivity(),VideoDetailActivity::class.java).apply {
                    this.putExtra("video",entity)
                }
                startActivity(intent)
            }
        }

        override fun getItemCount(): Int {
            return data.size
        }

        inner class VideoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val subBinding = ItemVideoBinding.bind(itemView)
        }
    }
}